﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
using MEF_Test.Bricks;
using Microsoft.Practices.Prism.Commands;
using Synology.AudioStationApi;

namespace PartTwo
{
    public class MainScreenViewModel : INotifyPropertyChanged
    {
        private readonly ISearchResultViewModelFactory _searchResultViewModelFactory;
        private IEnumerable<ISearchResultViewModel> _searchResults;

        /// <summary>
        /// THe name of the <see cref="SearchResults"/> property.
        /// </summary>
        private readonly string _searchResultsPropertyName = PropertyHelper.MemberName<MainScreenViewModel>(o => o.SearchResults);

        private readonly ISearchService _searchService;

        /// <summary>
        /// Gets or sets the search results.
        /// </summary>
        /// <value>The search results.</value>
        public IEnumerable<ISearchResultViewModel> SearchResults
        {
            get
            {
                return _searchResults;
            }
            private set
            {
                _searchResults = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(_searchResultsPropertyName));
                }
            }
        }

        public ObservableCollection<SynoTrack> Playlist { get; private set; }

        public ICommand SearchCommand { get; private set; }

        public MainScreenViewModel(ISearchService searchService, ObservableCollection<SynoTrack> playlist)
        {
            SearchCommand = new DelegateCommand<string>(OnSearchExecute, o=>true);
            _searchService = searchService;
            Playlist = playlist;
        }

        private void OnSearchExecute(string searchQuery)
        {
            SearchResults = _searchService.Search(searchQuery);
           // SearchResults = _session.RequestHamilcarServeftpCom5000BrowseCgi(searchQuery).Select(o=>_searchResultViewModelFactory.Create(o, _session));
        }

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
    }

    public interface ISearchService
    {
        IEnumerable<ISearchResultViewModel> Search(string searchQuery);
    }
}